<?php
/**
 * Fang_Controller_Message
 * 
 * 作者:  姚刚 (yaogang@the9sense.com)
 * 创建时间: 2011-03-19 08:09:53
 * 修改记录: 
 * 
 * $Id: Message.php 18 2011-04-17 12:50:07Z coosuu $
 */

class Fang_Controller_Message extends Fang_Controller_Base {

    /**
     * indexAction 
     * 列表页
     * 
     * @return void
     */
    public function indexAction() {

        $page = intval($this->request->get('page', 1));
        $pageOptions = array('currentPage' => $page,
                             'perPage' => 15);

        try {
            $messageService =& Service::factory('Message');
            $messages = $messageService->listPass($pageOptions);
        } catch (Exception $e) {
            return $this->errorMessage($e->getMessage());
        }

        $this->view->set('messages', $messages);
        $this->view->set('page', $pageOptions);
        return $this->view->render('messages/list');
    }

    /**
     * postAction 
     * 发表留言(注意留言需要在后台审核/回复)
     * 
     * @return void
     */
    public function postAction() {

        if ($this->request->isPost()) {
            $posts = $this->request->postx('nickname', 'content', 'email', 'mobile', 'qq');
            if (!$posts['nickname'] || !$posts['content']) {
                return $this->errorMessage('必须填写昵称和留言内容');
            }

            foreach ($posts as $k => $v) {
                $$k = strip_tags($v);
            }

            try {
                $messageService =& Service::factory('Message');
                $res = $messageService->add($nickname, $content, 0, $email, $mobile, $qq);
            } catch (Exception $e) {
                return $this->errorMessage($e->getMessage());
            }

            if (!$res) {
                return $this->errorMessage('留言发表失败，请稍后重试');
            }

            return $this->succMessage('恭喜您，留言发表成功，请耐心等待回复');
        }

        return $this->view->render('messages/post');
    }
}
